import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',  //初始化token
    permissionList: ''
  },
  mutations: {
    //存储token方法
    //设置token等于外部传递进来的值
    setToken(state, token) {
      state.token = token
      window.sessionStorage.token = token //同步存储token至sessionStorage
    },

    setpermissionList(state, permissionList) {
      state.permissionList = permissionList
      window.sessionStorage.permissionList = permissionList //同步存储token至sessionStorage
    },
  },
  getters: {
    //获取token方法
    //判断是否有token,如果没有重新赋值，返回给state的token
    getToken(state) {
      if (!state.token) {
        state.token = window.sessionStorage.getItem('token')
      }
      return state.token
    },

    getpermissionList(state) {
      if (!state.permissionList) {
        state.permissionList =window.sessionStorage.getItem('permissionList');
      }
      return JSON.parse(state.permissionList)
    }
  }
})